Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
"Cast to number failed for value ... at path ... " Error when using updateOne with $pop

I'm having this error and I'm not sure if this is an error in my query or how I set up the schema for this document. I can insert without problem though, to this document in the db. Now I'm trying to remove an element in cartItems field but can't do so.

Here is the error:

CastError: Cast to number failed for value "613997c799907eadff60cff1" (type string) at path "cartItems"

I set up my schema like this. And as much as possible I'd like it to stay that way because all of my other codes might get affected and I don't want that at the moment.

const userSchema = new mongoose.Schema({

cartItems:{
    
    type: Array
}})

Then here is the code that updates the db:

module.exports.removeFromCart = (userId,productId)=>{

                                                        
return User.updateOne({_id: userId}, {$pop: {cartItems:{item:productId}}}).then((result)=>{

    console.log(result);


}).catch(error=>{

    //THIS THROWS THE ERROR I"M GETTING
    console.log(error);
})}

What could I be doing wrong here? By the way, I can console.log all the data being passed to the query so I'm sure I'm not passing undefined data to mongoose query.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The $pop operator expects a number (1 or -1) after cartItems:, see the documentation.

To remove an element based on a filter, use $pull.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda